﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Gap.BllTest
{
    /// <summary>
    /// Summary description for BrisishGasCCTest
    /// </summary>
    [TestClass]
    public class BrisishGasCCTest
    {
        public BrisishGasCCTest()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void TestMethod1()
        {
            string schoolName = "schoolName";
            string className = "className";
            DateTime start = new DateTime(2008, 4, 1);
            DateTime end = new DateTime(2008, 5, 1);

            //delete if exists
            Bll.BritishGas.CarbonCalculation actual = Bll.BritishGas.CarbonCalculation.Get(schoolName, className, start, end);
            if (actual != null)
            {
                actual.Delete();
            }

            //create
            actual = Gap.Bll.BritishGas.CarbonCalculation.CreateInstance(schoolName, className, start, end);
            actual.Butane150 = 1;
            actual.Butane45 = 2;
            actual.Butane70 = 3;
            actual.ElectricityKwh = 4;
            actual.GasKwh = 5;
            actual.GreenElectricity = 6;
            actual.NoOfPupils = 7;
            actual.OilKwh = 8;
            actual.OilLitres = 9;
            actual.Propane130 = 10;
            actual.Propane39 = 11;
            actual.Propane60 = 12;
            actual.RenewableOther = 13;
            actual.SolarPanels = 14;
            actual.WindTurbine = 15;
            actual.RenewablePercentage = 0.1f;

            actual.CalculateFootprint();

            float butanePerPupil = actual.ButanePerPupil;
            float butaneTotal = actual.ButaneTotal;
            float electricityPerPupil = actual.ElectricityPerPupil;
            float electricityTotal = actual.ElectricityTotal;
            float gasPerPupil = actual.GasPerPupil;
            float gasTotal = actual.GasTotal;
            float oilKwhPerPupil = actual.OilKwhPerPupil;
            float oilKwhTotal = actual.OilKwhTotal;
            float propanePerPupil = actual.PropanePerPupil;
            float propaneTotal = actual.PropaneTotal;
            float tonnesPerPupil = actual.TonnesPerPupil;
            float tonnesTotal = actual.TonnesTotal;

            actual.SaveChanges();

            Assert.AreEqual(schoolName, actual.SchoolId);
            Assert.AreEqual(className, actual.ClassName);
            Assert.AreEqual(start, actual.Start);
            Assert.AreEqual(end, actual.End);
            Assert.AreEqual(1f, actual.Butane150.Value, 0.01f);
            Assert.AreEqual(2f, actual.Butane45.Value, 0.01f);
            Assert.AreEqual(3f, actual.Butane70.Value, 0.01f);
            Assert.AreEqual(4f, actual.ElectricityKwh.Value, 0.01f);
            Assert.AreEqual(5f, actual.GasKwh.Value, 0.01f);
            Assert.AreEqual(6f, actual.GreenElectricity.Value, 0.01f);
            Assert.AreEqual(7, actual.NoOfPupils);
            Assert.AreEqual(8f, actual.OilKwh.Value, 0.01f);
            Assert.AreEqual(9f, actual.OilLitres.Value, 0.01f);
            Assert.AreEqual(10f, actual.Propane130.Value, 0.01f);
            Assert.AreEqual(11f, actual.Propane39.Value, 0.01f);
            Assert.AreEqual(12f, actual.Propane60.Value, 0.01f);
            Assert.AreEqual(13f, actual.RenewableOther.Value, 0.01f);
            Assert.AreEqual(14f, actual.SolarPanels.Value, 0.01f);
            Assert.AreEqual(15f, actual.WindTurbine.Value, 0.01f);
            Assert.AreEqual(0.1, actual.RenewablePercentage.Value, 0.01f);
            Assert.AreEqual(butanePerPupil, actual.ButanePerPupil, 0.01f);
            Assert.AreEqual(butaneTotal, actual.ButaneTotal, 0.01f);
            Assert.AreEqual(electricityPerPupil, actual.ElectricityPerPupil, 0.01f);
            Assert.AreEqual(electricityTotal, actual.ElectricityTotal, 0.01f);
            Assert.AreEqual(gasPerPupil, actual.GasPerPupil, 0.01f);
            Assert.AreEqual(gasTotal, actual.GasTotal, 0.01f);
            Assert.AreEqual(oilKwhPerPupil, actual.OilKwhPerPupil, 0.01f);
            Assert.AreEqual(oilKwhTotal, actual.OilKwhTotal, 0.01f);
            Assert.AreEqual(propanePerPupil, actual.PropanePerPupil, 0.01f);
            Assert.AreEqual(propaneTotal, actual.PropaneTotal, 0.01f);
            Assert.AreEqual(tonnesPerPupil, actual.TonnesPerPupil, 0.01f);
            Assert.AreEqual(tonnesTotal, actual.TonnesTotal, 0.01f);

            //read
            actual = Bll.BritishGas.CarbonCalculation.Get(schoolName, className, start, end);

            Assert.IsNotNull(actual);

            Assert.AreEqual(schoolName, actual.SchoolId);
            Assert.AreEqual(className, actual.ClassName);
            Assert.AreEqual(start, actual.Start);
            Assert.AreEqual(end, actual.End);
            Assert.AreEqual(1f, actual.Butane150.Value, 0.01f);
            Assert.AreEqual(2f, actual.Butane45.Value, 0.01f);
            Assert.AreEqual(3f, actual.Butane70.Value, 0.01f);
            Assert.AreEqual(4f, actual.ElectricityKwh.Value, 0.01f);
            Assert.AreEqual(5f, actual.GasKwh.Value, 0.01f);
            Assert.AreEqual(6f, actual.GreenElectricity.Value, 0.01f);
            Assert.AreEqual(7, actual.NoOfPupils);
            Assert.AreEqual(8f, actual.OilKwh.Value, 0.01f);
            Assert.AreEqual(9f, actual.OilLitres.Value, 0.01f);
            Assert.AreEqual(10f, actual.Propane130.Value, 0.01f);
            Assert.AreEqual(11f, actual.Propane39.Value, 0.01f);
            Assert.AreEqual(12f, actual.Propane60.Value, 0.01f);
            Assert.AreEqual(13f, actual.RenewableOther.Value, 0.01f);
            Assert.AreEqual(14f, actual.SolarPanels.Value, 0.01f);
            Assert.AreEqual(15f, actual.WindTurbine.Value, 0.01f);
            Assert.AreEqual(0.1f, actual.RenewablePercentage.Value, 0.01f);
            Assert.AreEqual(butanePerPupil, actual.ButanePerPupil, 0.01f);
            Assert.AreEqual(butaneTotal, actual.ButaneTotal, 0.01f);
            Assert.AreEqual(electricityPerPupil, actual.ElectricityPerPupil, 0.01f);
            Assert.AreEqual(electricityTotal, actual.ElectricityTotal, 0.01f);
            Assert.AreEqual(gasPerPupil, actual.GasPerPupil, 0.01f);
            Assert.AreEqual(gasTotal, actual.GasTotal, 0.01f);
            Assert.AreEqual(oilKwhPerPupil, actual.OilKwhPerPupil, 0.01f);
            Assert.AreEqual(oilKwhTotal, actual.OilKwhTotal, 0.01f);
            Assert.AreEqual(propanePerPupil, actual.PropanePerPupil, 0.01f);
            Assert.AreEqual(propaneTotal, actual.PropaneTotal, 0.01f);
            Assert.AreEqual(tonnesPerPupil, actual.TonnesPerPupil, 0.01f);
            Assert.AreEqual(tonnesTotal, actual.TonnesTotal, 0.01f);

            actual.Butane150 = null;
            actual.Butane45 = null;
            actual.Butane70 = null;
            actual.ElectricityKwh = null;
            actual.GasKwh = null;
            actual.GreenElectricity = null;
            actual.NoOfPupils = 0;
            actual.OilKwh = null;
            actual.OilLitres = null;
            actual.Propane130 = null;
            actual.Propane39 = null;
            actual.Propane60 = null;
            actual.RenewableOther = null;
            actual.RenewablePercentage = null;
            actual.SolarPanels = null;
            actual.WindTurbine = null;
            actual.CalculateFootprint();

            actual.SaveChanges();

            Assert.AreEqual(schoolName, actual.SchoolId);
            Assert.AreEqual(className, actual.ClassName);
            Assert.AreEqual(start, actual.Start);
            Assert.AreEqual(end, actual.End);
            Assert.IsNull(actual.Butane150);
            Assert.IsNull(actual.Butane45);
            Assert.IsNull(actual.Butane70);
            Assert.IsNull(actual.ElectricityKwh);
            Assert.IsNull(actual.GasKwh);
            Assert.IsNull(actual.GreenElectricity);
            Assert.AreEqual(0, actual.NoOfPupils);
            Assert.IsNull(actual.OilKwh);
            Assert.IsNull(actual.OilLitres);
            Assert.IsNull(actual.Propane130);
            Assert.IsNull(actual.Propane39);
            Assert.IsNull(actual.Propane60);
            Assert.IsNull(actual.RenewableOther);
            Assert.IsNull(actual.SolarPanels);
            Assert.IsNull(actual.WindTurbine);
            Assert.IsNull(actual.RenewablePercentage);
            Assert.AreEqual(0f, actual.ButanePerPupil, 0.01f);
            Assert.AreEqual(0f, actual.ButaneTotal, 0.01f);
            Assert.AreEqual(0f, actual.ElectricityPerPupil, 0.01f);
            Assert.AreEqual(0f, actual.ElectricityTotal, 0.01f);
            Assert.AreEqual(0f, actual.GasPerPupil, 0.01f);
            Assert.AreEqual(0f, actual.GasTotal, 0.01f);
            Assert.AreEqual(0f, actual.OilKwhPerPupil, 0.01f);
            Assert.AreEqual(0f, actual.OilKwhTotal, 0.01f);
            Assert.AreEqual(0f, actual.PropanePerPupil, 0.01f);
            Assert.AreEqual(0f, actual.PropaneTotal, 0.01f);
            Assert.AreEqual(0f, actual.TonnesPerPupil, 0.01f);
            Assert.AreEqual(0f, actual.TonnesTotal, 0.01f);

            actual = Bll.BritishGas.CarbonCalculation.Get(schoolName, className, start, end);

            Assert.AreEqual(schoolName, actual.SchoolId);
            Assert.AreEqual(className, actual.ClassName);
            Assert.AreEqual(start, actual.Start);
            Assert.AreEqual(end, actual.End);
            Assert.IsNull(actual.Butane150);
            Assert.IsNull(actual.Butane45);
            Assert.IsNull(actual.Butane70);
            Assert.IsNull(actual.ElectricityKwh);
            Assert.IsNull(actual.GasKwh);
            Assert.IsNull(actual.GreenElectricity);
            Assert.AreEqual(0, actual.NoOfPupils);
            Assert.IsNull(actual.OilKwh);
            Assert.IsNull(actual.OilLitres);
            Assert.IsNull(actual.Propane130);
            Assert.IsNull(actual.Propane39);
            Assert.IsNull(actual.Propane60);
            Assert.IsNull(actual.RenewableOther);
            Assert.IsNull(actual.SolarPanels);
            Assert.IsNull(actual.WindTurbine);
            Assert.IsNull(actual.RenewablePercentage);
            Assert.AreEqual(0f, actual.ButanePerPupil, 0.01f);
            Assert.AreEqual(0f, actual.ButaneTotal, 0.01f);
            Assert.AreEqual(0f, actual.ElectricityPerPupil, 0.01f);
            Assert.AreEqual(0f, actual.ElectricityTotal, 0.01f);
            Assert.AreEqual(0f, actual.GasPerPupil, 0.01f);
            Assert.AreEqual(0f, actual.GasTotal, 0.01f);
            Assert.AreEqual(0f, actual.OilKwhPerPupil, 0.01f);
            Assert.AreEqual(0f, actual.OilKwhTotal, 0.01f);
            Assert.AreEqual(0f, actual.PropanePerPupil, 0.01f);
            Assert.AreEqual(0f, actual.PropaneTotal, 0.01f);
            Assert.AreEqual(0f, actual.TonnesPerPupil, 0.01f);
            Assert.AreEqual(0f, actual.TonnesTotal, 0.01f);
            
            actual.Delete();

            actual = Bll.BritishGas.CarbonCalculation.Get(schoolName, className, start, end);

            Assert.IsNull(actual);
        }

        [TestMethod]
        public void CalcFootprintTest()
        {
            float margin = 0.01f;
            Gap.Bll.BritishGas.CarbonCalculation tmp = new Gap.Bll.BritishGas.CarbonCalculation();
            tmp.ElectricityKwh = 10000;
            tmp.RenewablePercentage = 0.12f;
            tmp.GasKwh = 12f;
            tmp.OilKwh = 13f;
            tmp.OilLitres = 5f;
            tmp.Butane45 = 2f;
            tmp.Butane70 = 15f;
            tmp.Butane150 = 14f;
            tmp.Propane39 = 4f;
            tmp.Propane60 = 5f;
            tmp.Propane130 = 1f;

            tmp.NoOfPupils = 100;

            tmp.CalculateFootprint();

            Assert.AreEqual(4602.40, tmp.ElectricityTotal, margin);
            Assert.AreEqual(2.22, tmp.GasTotal, margin);
            Assert.AreEqual(19.09, tmp.OilKwhTotal, margin);
            Assert.AreEqual(909.05, tmp.ButaneTotal, margin);
            Assert.AreEqual(166.95, tmp.PropaneTotal, margin);
            Assert.AreEqual(5699.71, tmp.TonnesTotal, margin);

            Assert.AreEqual(46.02, tmp.ElectricityPerPupil, margin);
            Assert.AreEqual(0.02, tmp.GasPerPupil, margin);
            Assert.AreEqual(0.19, tmp.OilKwhPerPupil, margin);
            Assert.AreEqual(9.09, tmp.ButanePerPupil, margin);
            Assert.AreEqual(1.67, tmp.PropanePerPupil, margin);
            Assert.AreEqual(57.00, tmp.TonnesPerPupil, margin);

        }
    }
}
